package com.wu.http.server;

import com.wu.javax.servlet.http.HttpSession;
import com.wu.javax.servlet.http.HttpSessionContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: tomcatServer
 * @author: 吴磊
 * @create: 2021-12-31 14:14
 */
public class MyHttpSessionContext implements HttpSessionContext {

    private static final Map<String, HttpSession> SESSION_MAP = new ConcurrentHashMap<>();
    private static HttpSessionContext httpSessionContext;

    public Map<String, HttpSession> getSessions() {
        return SESSION_MAP;
    }

    private MyHttpSessionContext() {
    }

    /**
     * 单例
     *
     * @return
     */
    public synchronized static HttpSessionContext getInstance() {
        if (httpSessionContext == null) {
            httpSessionContext = new MyHttpSessionContext();
        }
        return httpSessionContext;
    }

    @Override
    public HttpSession getSession(String sessionId) {
        return SESSION_MAP.get(sessionId);
    }

    @Override
    public void setSession(String sessionId, HttpSession session) {
        SESSION_MAP.put(sessionId, session);
    }
}
